#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<errno.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define DEFAULTPORT         2345
#define DEFAULT_BACKLOG     20
#define BUFFSIZE            100

typedef unsigned int UINT32;
typedef unsigned short UINT16;
typedef unsigned char UINT8;

typedef UINT16     TC_OPCODE_T;

typedef struct 
{
    UINT8        reserved1         : 7;  // Reserved
    UINT8        cmd_rsp_flag      : 1;  // Command/Response Flag  0 indicate command   1 response
    UINT8        seq_tag;                // Sequence Tag   // ensure the same command between request and response
    TC_OPCODE_T  opcode;                 // Opcode
    UINT8        client_tag;              // Reserved
    UINT8        no_rsp_reqd_flag  : 1;  // No Response Required Flag   0 expected response 1 no expected
    UINT8        reserved3         : 7;  // Reserved
    UINT16       reserved4;              // Reserved
    UINT32       length;                 // Data Length of Request
} TC_CMD_REQ_HDR_T;

int main(int argc ,char *argv[])
{
    char buf[BUFFSIZE] = {0};
    int recv_len;
    int data_total_len = 0;

    struct sockaddr_in socket_addr;
    struct hostent *host;
    FILE *p;
	TC_CMD_REQ_HDR_T rev_info;
    char set_buff[13] = {0};
    set_buff[1] = 0x5A;
    set_buff[2] = 0x01;
    set_buff[4] = 0x0A;
    memcpy(&(rev_info), set_buff, sizeof(TC_CMD_REQ_HDR_T));

    printf("----tc_recv_socket----------header opcode---0x%x\n", rev_info.opcode);
    printf("----tc_recv_socket----------rev_info.length ---0x%d\n", rev_info.length);
    printf("----tc_recv_socket----------rev_info.client_tag ---0x%d\n", rev_info.client_tag);

    p = popen("adb forward tcp:2345 tcp:21480", "r");
    if (p == NULL)
    { 
        printf("popen() error!\n"); 
        exit(1); 
    }
    if((host=gethostbyname("127.0.0.1")) == NULL) {
        herror("gethostbyname error !");
        exit(1);
    }

    socket_addr.sin_family = AF_INET;
    socket_addr.sin_port = htons(DEFAULTPORT);
    socket_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(socket_addr.sin_zero), 8);

    int i = 0;
    int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
    if (socket_fd == -1)
        printf("Socket Error log is : %s\n", strerror(errno));

    if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr)) == -1) {
        perror("connect error !");
        exit(1);
    }
    if (send(socket_fd, set_buff, 8, 0) == -1){
        perror("send error !");
        close(socket_fd);
    }
    while(1){
        recv_len = recv(socket_fd, buf, sizeof(buf), 0);
        switch(recv_len){
            case -1:
                printf("Recv Error log is : %s\n", strerror(errno));
                close(socket_fd);
                exit(0);
            case 0:
                printf("other side socket is closed !\n");
            default:
                data_total_len += recv_len;
                printf("recv data len is %d, data is = %s\n", data_total_len,  (buf+12));
                for(;i<data_total_len;i++)
                    printf("buf[%d] = %x\n", i, buf[i]);
                memset(buf, 0, sizeof(buf));
        }
    }
        return 0;
}
